import tornado.web
import json

class GenderHandler(tornado.web.RequestHandler):
    def get(self):
        self.render('gender.html')

class gender_data(tornado.web.RequestHandler):
    def get(self):
        import pymysql 
        conn = pymysql.connect(
            host="localhost",
            user="root",password="628925yiting",
            database="zhihu",
            charset="utf8")
        cursor = conn.cursor()
        sql = "SELECT * FROM china_sex"
        cursor.execute(sql)
        result = cursor.fetchall()
        print(result)
        cursor.close()
        conn.close()
        jsonData =[]
        for row in data:
            result ={}
            result['gender']=str[row[0]]
            result['Unknow']=row[1]
            result['male']=row[2]
            result['female']=row[3]
            jsonData.append(result)
            print (u'转换为列表字典的原始数据：',jsonData)
        else:
            jsondatar=json.dumps(jsonData,ensure_ascii=False)
            return jsondatar[1:len(jsondatar)-1]
            
    if __name__ == '__main__': 
     jsonData = TableToJson() 
     print (u'转换为json格式的数据：',jsonData) 
     #以读写方式w+打开文件，路径前加r，防止字符转义
     f = open(r'C:\\Users\\Administrator\\Desktop\\Project\\static\\json\\gender.txt','w+')
     #写数据
     f.write(jsonData)
     #关闭文件 
     f.close()
     self.finish(result)
